<template>
  <div>
    <van-nav-bar title="登录" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button square block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <div>
      <router-link to="/login" class="link">没有账号，先注册</router-link>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
import { Toast } from 'vant'
export default {
  name: 'login-page',
  methods: {
    async onSubmit (values) {
      console.log(values)
      const { body } = await login(values)
      setToken(body.token)
      this.$toast.success('登录成功')
      this.$router.push('/')
    },
    onClickLeft () {
      Toast('返回')
    }
  },
  data () {
    return {
      username: '',
      password: ''
    }
  }
}
</script>

<style lang='less' scoped>
.link {
  color:'skyblue';
  float: right;
  padding-right: 20px;
  font-size: 16px;
}
</style>
